<?php


require 'defaultController.php';

class taskController extends defaultController {

    function indexAction() {
        if($this->checkLogin()){
             $this->_view('addtask.tpl.php');
        }else{
            $this->redirectPage("index.php?c=login&a=index");
        }   
    }
    
     function index2Action() {
        if($this->checkLogin()){
             $this->_view('addtask2.tpl.php');
        }else{
            $this->redirectPage("index.php?c=login&a=index");
        }   
    }

    
    function listAction(){
        if(!$this->checkLogin()){
             $this->redirectPage("index.php?c=login&a=index");
        }
        $id=$_SESSION['userid'];
        if($this->for_get('class')){
            $list=$this->_model('task')->memberall2($id,$this->for_get('class'));
        }else{
            $list=$this->_model('task')->memberall($id);
        }            
        $data['list']=$list;
        $this->_view('tasklist.tpl.php',$data);
    }
    
    function infoAction(){
        $id=$this->for_get('id');
        $info=$this->_model('task')->info($id);   
        $member=$this->_model('login')->getinfo($info['member_id']);
        $data['info']=$info;
        $data['member']=$member;
        $this->_view('taskinfo.tpl.php',$data);
    }
    
    function delAction(){
        $id=$this->for_get('id');
        $result=$this->_model('task')->del($id);
        if($result){
           $this->redirectPage("index.php?c=task&a=list");
        }
    }

    function addAction() {
        $title = $this->for_post('title');
        $url = $this->for_post('itemurl');
        $sku = $this->for_post('skus');
        $count = $this->for_post('shopCount');
        $price = $this->for_post('price');
        $ischangeprice = $this->for_post('isPriceFit');
        $isget = $this->for_post('isZhitongche');
        $taskcount = $this->for_post('count');
        $servicecharge = $this->for_post('servicecharge');
        $gettimes = $this->for_post('times');
        $scores = $this->for_post('scores');
        $member_id=$_SESSION['userid'];
        $time=date("Y/m/d");
        $money=($price+10)*$taskcount;
        $data = array(
            "title" => $title,
            "url" => $url,
            "sku" => $sku,
            "count" => $count,
            "price" => $price,
            "ischangeprice" => $ischangeprice,
            "isget" => $isget,
            "taskcount" => $taskcount,
            "servicecharge" => $servicecharge,
            "gettimes" => $gettimes,
            "scores" => $scores,
            "member_id" => $member_id,
            "time"=>$time,
            "money"=>$money
        );
        $result=$this->_model('task')->add($data);
        if($result){
            $data=array(
                'title'=> "发布任务".'+'.$taskcount,
                'time'=> date("Y/m/d"),
                'member_id'=>$_SESSION['userid'],
                'class'=>3
            );
            $this->_model('data')->add($data);
            $this->redirectPage("index.php?c=task&a=info&id=".$result);
        }else{
            $this->showMessage("发布错误");
        }
    }
    
    function checkoutAction(){
        if(!$this->checkLogin()){
             $this->redirectPage("index.php?c=login&a=index");
        }
        $mid=$_SESSION['userid'];
        $tid=$this->for_get('id');
        $member=$this->_model('login')->getinfo($mid);
        $task=$this->_model('task')->info($tid);
        if($task['money']>=$member['money']){
            $this->showMessage("金币不足");
        }else{
            $money=$member['money']-$task['money'];
            $data=array(
                'money'=>$money
            );
            $data2=array(
                'status'=> 2
            );
            $result=$this->_model('login')-> updatemember($data);
            $result2=$this->_model('task')-> update($data2,$tid);
            if($result&&$result2){
                
                //付款成功 给推荐好友添加积分
                $info=$this->_model('login')-> getinfo($mid);
                $friendid=$info['friend'];
                $info2=$this->_model('login')-> getinfo($friendid);
                $mrank=$info2['mrank'];
                if($friendid){
                    $data=array(
                        'mrank'=>$task['taskcount']+$mrank
                    );
                    $this->_model('login')-> updatemember2($data,$friendid);
                }
                
                //添加日志
                $data=array(
                    'title'=> "任务付款".$task['money'],
                    'time'=> date("Y/m/d"),
                    'member_id'=>$_SESSION['userid'],
                    'class'=>1
                );
                $this->_model('data')->add($data);
                
                //添加平台积分
                $data=array(
                    'wrank'=> $info['wrank']+$task['taskcount']
                );
                $this->_model('login')-> updatemember($data);
                
                $this->redirectPage("index.php?c=task&a=info&id=".$tid);
            }else{
                $this->showMessage("付款错误");
            }
        }
        
    }

}
